/*
采用 getaddrinfo 传入主机名和服务名实现的 TCP 时间获取客户端。
**/

#include <define.h>
#include <error.h>
#include <wrapfunc.h>
#include <sockfunc.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <time.h>


int
main(int argc, char **argv) {
    if (argc != 3)
        error_quit("usage: %s host servname", argv[0]);
    const char *host = argv[1];
    const char *servname = argv[2];

    int sockfd = Tcp_connect(host, servname);
    struct sockaddr_storage ss;
    socklen_t len = sizeof(ss);
    Getpeername(sockfd, (struct sockaddr*)&ss, &len);
    error_msg("connected to %s", sock_ntop((struct sockaddr*)&ss, len));

    char buf[MAX_LINE];
    ssize_t n = Read(sockfd, buf, sizeof(buf));
    buf[n] = 0;
    error_msg("server time:%s", buf);

    Close(sockfd);
    return 0;
}
